package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {

    TeacherService teacherService = new TeacherService();
    Scanner sc = new Scanner(System.in);

    public void start() {
        System.out.println("--------欢迎来到 <老师> 管理系统--------");
        System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
        while (true) {
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除老师");
                    deleteTeacher();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("退出");
                    return;
                default:
                    System.out.println("您的输入有误，请重新输入");
            }

        }
    }

    public void updateTeacher() {
        String updateId;
        while (true) {
            System.out.println("请输入要修改的学号");
            updateId = sc.next();
            boolean exists = teacherService.isExists(updateId);
            if(!exists) {
                System.out.println("学号不存在，请重新输入");
            }else{
               break;
            }
        }
        System.out.println("请输入姓名");
        String updeName = sc.next();
        System.out.println("请输入年龄");
        String updeAge = sc.next();
        System.out.println("请输入生日");
        String updeBirthday = sc.next();
        Teacher t = new Teacher();
        t.setId(updateId);
        t.setName(updeName);
        t.setAge(updeAge);
        t.setBirthday(updeBirthday);
        teacherService.updateTeacher(updateId,t);
        System.out.println("修改成功");
    }

    public void deleteTeacher() {
        String delId;
        while (true) {
            System.out.println("请输入要删除的学号");
            delId = sc.next();
            boolean exists = teacherService.isExists(delId);
            if (!exists) {
                System.out.println("学号不存在，请重新输入");
            } else {
                break;
            }
        }
        teacherService.deleteTeacher(delId);
        System.out.println("删除成功");
    }

    public void findAllTeacher() {
        Teacher[] teachers = teacherService.findAllTeacher();
        if (teachers == null) {
            System.out.println("查无信息，请添加后再查找");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher t = teachers[i];
            if (t != null) {
                System.out.println(t.getId() + "\t" + t.getName() + "\t" + t.getAge() + "\t\t" + t.getBirthday());
            }
        }
    }

    public void addTeacher() {
        String id;
        while (true) {
            System.out.println("请输入学号");
            id = sc.next();
            boolean flag = teacherService.isExists(id);
            if (flag) {
                System.out.println("存在，请重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        String age = sc.next();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Teacher t = new Teacher();
        t.setId(id);
        t.setName(name);
        t.setAge(age);
        t.setBirthday(birthday);
        boolean flag = teacherService.addTeacher(t);
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }
}
